package com.graduation.service;

import com.graduation.dao.CartDao;
import com.graduation.dao.GoodsDao;
import com.graduation.entity.Cart;
import com.graduation.entity.Recommend;
import com.graduation.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Resource
    private CartDao cartDao;
    @Resource
    private GoodsDao goodsDao;

    @Override
    public Cart queryGoodsInfo(String id,Integer num,String email) {
        Recommend recommend = goodsDao.queryRecommendById(id);
        Cart cart = new Cart();
        cart.setFilename(recommend.getFilename());
        cart.setGoodsname(recommend.getGoodsName());
        cart.setPrice(recommend.getPrice());
        cart.setGoodsid(Integer.valueOf(id));
        cart.setShopname(recommend.getShopName());
        cart.setAddtime(DateUtil.getCurrentDate());
        cart.setEmail(email);
        //判断数量
        if(num == null || num == 0){
            num = 1;
        }
        /*判断是否存在*/
        Cart c = cartDao.queryIsExist(id, email);
        //存在
        if(c != null){
            if((c.getGoodsid() == Integer.valueOf(id)) && (c.getEmail().equals(email))){
                cartDao.updateCartById(id,num, Double.valueOf(recommend.getPrice()));
                return cart;
            }
        }
        cart.setNum(String.valueOf(num));
        Integer integer = cartDao.addGoodsToCart(cart);
        if(integer == 1){
            return cart;
        }
        /**
         * 返回空对象
         */
        return new Cart();
    }

    @Override
    public List<Cart> queryGoodsByEmail(String email) {
        return cartDao.queryGoodsByEmail(email);
    }

    @Override
    public Integer deleteGoodsById(String id) {
        return cartDao.deleteGoodsById(id);
    }
}
